package com.bodu.system.controller;

import com.bodu.core.common.AbstractCommonController;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.result.PageResult;
import com.bodu.core.entity.result.Result;
import com.bodu.global.annon.TraceLog;
import com.bodu.global.security.LoginUserUtil;
import com.bodu.system.entity.request.CronTriggersRequest;
import com.bodu.system.entity.vo.CronTriggersVo;
import com.bodu.system.service.CronTriggersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定时器 API
 *
 * @author Administrator
 */
@Api(tags = "定时器 API")
@RestController
@RequestMapping("/cronTriggers")
public class CronTriggersController extends AbstractCommonController<CronTriggersService, CronTriggersRequest, CronTriggersVo> {

    private final CronTriggersService cronTriggersService;

    public CronTriggersController(CronTriggersService cronTriggersService) {
        this.cronTriggersService = cronTriggersService;
    }

    @TraceLog()
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(cronTriggersService, id);
    }

    @TraceLog()
    @PostMapping
    @ApiOperation("保存定时器")
    public Result<Integer> save(@RequestBody CronTriggersRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.save(cronTriggersService, request);
    }

    @TraceLog()
    @PutMapping
    @ApiOperation("更新定时器")
    public Result<Integer> update(@RequestBody CronTriggersRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.update(cronTriggersService, request);
    }

    @TraceLog()
    @PostMapping("listPage")
    @ApiOperation("定时器-查询")
    public Result<List<CronTriggersVo>> listPage(@RequestBody ParamReq req) {
        req.setManagerDept(LoginUserUtil.getPrincipal().getManagerDept());
        return super.listPage(cronTriggersService, req);
    }

    @TraceLog()
    @PostMapping("countPage")
    @ApiOperation("定时器-统计")
    public Result<Integer> countPage(@RequestBody ParamReq req) {
        req.setManagerDept(LoginUserUtil.getPrincipal().getManagerDept());
        return super.countPage(cronTriggersService, req);
    }

    @TraceLog()
    @PostMapping("page")
    @ApiOperation("定时器-分页数据")
    public Result<PageResult> page(@RequestBody ParamReq<CronTriggersRequest> req) {
        req.setManagerDept(LoginUserUtil.getPrincipal().getManagerDept());
        return super.page(cronTriggersService, req);
    }

    @TraceLog()
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除定时器")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return super.delete(cronTriggersService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除定时器")
    @DeleteMapping("{ids}")
    public Result<Integer> batchDelete(List<Long> ids) {
        return super.batchDelete(cronTriggersService, ids);
    }

    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }


    /**
     * 存储状态只能存在一个起作用
     * @return
     */
    @TraceLog()
    @ApiOperation("定时任务切换状态")
    @PostMapping("/switchStatus")
    public Result<Integer> switchStatus(@RequestBody CronTriggersRequest request){
        return cronTriggersService.switchStatus(request);
    }


}
